package com.taosdata.example.springbootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taosdata.example.springbootdemo.dao.mysql.MysqlMapper;
import com.taosdata.example.springbootdemo.dao.tdengine.TaosMapper;
import com.taosdata.example.springbootdemo.domain.Weather;
import com.taosdata.example.springbootdemo.service.IWeatherService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;


@Service
@Log4j2
public class WeatherServiceImpl implements IWeatherService {

    @Autowired
    private TaosMapper taosMapper;

    @Autowired
    private MysqlMapper mysqlMapper;

    @Override
    public List<Weather> all() {
        System.out.println("**************** taos ****************");
        List<Weather> all = taosMapper.all();
        System.out.println(all);

//        System.out.println("**************** mysql ****************");
//        List<Weather> weatherList = mysqlMapper.findAll();
//        System.out.println(weatherList);

        System.out.println("**************** WeatherServiceImpl.all over ****************");
        return all;
    }

    @Override
    public String add1w() {
        long start = System.currentTimeMillis();
        long end = System.currentTimeMillis();
        int a = 0;
        try {
            for (int i = 0; i < 20000; i++) {
                Weather weather = new Weather();
                weather.setHumidity(i);
                weather.setTemperature(i);
                weather.setTs(Timestamp.valueOf(LocalDateTime.now()));
                end = System.currentTimeMillis();
                a = i;
                taosMapper.save(weather);
            }
        } catch (Exception e) {
            System.out.println("*****************************");
            System.out.println("用时:" + (end - start) + "毫秒");
            System.out.println(a);
            System.out.println("*****************************");
            e.printStackTrace();
        } finally {


        }


        return "插入1万数据成功";
    }

    @Override
    public Weather findOne() {
        return taosMapper.selectOne(new QueryWrapper<Weather>().eq("temperature", 1000));
    }
}
